package resource 
{
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author carlor
	 */
	public class ImageResource extends Resource 
	{
		private var _loader: Loader;
		private var _data  : BitmapData;
		
		override public function load(fileName: String): void
		{
			super.load(fileName);
			
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			_loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadSecurityError);
			_loader.load(new URLRequest(fileName));
		}
		
		override public function unLoad(): void
		{
			_loader.unloadAndStop();
			_loader.unload();
			_loader = null;
			
			if (_data) _data.dispose();
			_data = null;
		}
		
		override protected function onLoadComplete(e: Event): void
		{
			_data = e.currentTarget.content.bitmapData as BitmapData;
			super.onLoadComplete(e);
		}
		
		public function get data(): BitmapData
		{
			return _data;
		}
	}

}